package com.besmart.workflow.core.model;

import java.util.ArrayList;
import java.util.List;

public class WorkflowPool {

	public static final int ADD_FAILURE = 0;
	public static final int ADD_SUCCESS = 1;

	private List<WorkflowNode> pool;

	public WorkflowPool() {
		this.pool = new ArrayList<WorkflowNode>();
	}

	public OperationNode getOperationWithOperationIndex(
			OperationIndex operationIndex) {
		WorkflowNode workflow = getWorkflowById(operationIndex.getWorkflowId());
		if (workflow == null) {
			return null;
		} else {
			OperationNode operation = workflow.getOperationById(operationIndex
					.getOperationId());
			return operation;
		}
	}

	private WorkflowNode getWorkflowById(String workflowId) {
		for (WorkflowNode node : pool) {
			if (node.getId().equals(workflowId)) {
				return node;
			}
		}
		return null;
	}

	public int addWorkflowNode(WorkflowNode workflowNode) {
		int returnCode = ADD_SUCCESS;
		for (WorkflowNode node : pool) {
			if (node.getId().equals(workflowNode.getId())) {
				returnCode = ADD_FAILURE;
				break;
			}
		}
		if (returnCode == ADD_SUCCESS) {
			this.pool.add(workflowNode);
			return returnCode;
		} else
			return returnCode;
	}

	public List<WorkflowNode> getPool() {
		return pool;
	}
}
